package com.fwj.mvc.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Vector;

/**
 * Created by dreamsun on 16-6-9.
 */
public class DateUtils {

    public final static String YYYY = "yyyy";
    public final static String MM = "MM";
    public final static String DD = "dd";
    public final static String YYYY_MM_DD = "yyyy-MM-dd";
    public final static String YYYY_MM = "yyyy-MM";
    public final static String HH_MM_SS = "HH:mm:ss";
    public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String formatStr_yyyyMMddHHmmssS = "yyyy-MM-dd HH:mm:ss.S";
    public static String formatStr_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static String formatStr_yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
    public static String formatStr_yyyyMMddHH = "yyyy-MM-dd HH";
    public static String formatStr_yyyyMMdd = "yyyy-MM-dd";
    public static String[] formatStr = {
            formatStr_yyyyMMddHHmmss,
            formatStr_yyyyMMddHHmm,
            formatStr_yyyyMMddHH,
            formatStr_yyyyMMdd
    };

    /**
     * 构造函数
     */
    public DateUtils() {
    }

    /**
     * 日期格式化－将<code>Date</code>类型的日期格式化为<code>String</code>型
     *
     * @param date    待格式化的日期
     * @param pattern 时间样式  YYYY-mm-dd HH:MM:SS
     * @return 一个被格式化了的<code>String</code>日期
     */
    public static String format(Long date, String pattern) {
        return format(new Date(date), pattern);
    }

    /**
     * 日期格式化－将<code>Date</code>类型的日期格式化为<code>String</code>型
     *
     * @param date    待格式化的日期
     * @param pattern 时间样式
     * @return 一个被格式化了的<code>String</code>日期
     */
    public static String format(Date date, String pattern) {
        if (date == null) return "";
        else return getFormatter(pattern).format(date);
    }

    /**
     * 默认把日期格式化成yyyy-mm-dd格式
     *
     * @param date
     * @return
     */
    public static String format(Date date) {
        if (date == null)
            return "";
        else
            return getFormatter(YYYY_MM_DD).format(date);
    }

    /**
     * 把字符串日期默认转换为yyyy-mm-dd格式的Data对象
     *
     * @param strDate
     * @return
     */
    public static Date format(String strDate) {
        Date d = null;
        if (strDate == "")
            return null;
        else
            try {
                d = getFormatter(YYYY_MM_DD).parse(strDate);
            } catch (ParseException pex) {
                return null;
            }
        return d;
    }

    /**
     * 把字符串日期转换为f指定格式的Data对象
     *
     * @param strDate,f
     * @return
     */
    public static Date format(String strDate, String f) {
        Date d = null;
        if (strDate == "")
            return null;
        else
            try {
                d = getFormatter(f).parse(strDate);
            } catch (ParseException pex) {
                return null;
            }
        return d;
    }

    /**
     * 日期解析－将<code>String</code>类型的日期解析为<code>Date</code>型
     *
     * @param strDate 待格式化的日期
     * @param pattern 日期样式
     * @return 一个被格式化了的<code>Date</code>日期
     * @throws ParseException 如果所给的字符串不能被解析成一个日期
     */
    public static Date parse(String strDate, String pattern) throws
            ParseException {
        try {
            return getFormatter(pattern).parse(strDate);
        } catch (ParseException pe) {
            throw new ParseException(
                    "Method parse in Class DateUtils  err: parse strDate fail.",
                    pe.getErrorOffset());
        }
    }

    /**
     * 获取当前日期
     *
     * @return 一个包含年月日的<code>Date</code>型日期
     */
    public static synchronized Date getCurrDate() {
        Calendar calendar = Calendar.getInstance();
        return calendar.getTime();
    }

    /**
     * 获取当前日期
     *
     * @return 一个包含年月日的<code>String</code>型日期，但不包含时分秒。yyyy-mm-dd
     */
    public static String getCurrDateStr() {
        return format(getCurrDate(), YYYY_MM_DD);
    }

    /**
     * 获取当前时间
     *
     * @return 一个包含年月日时分秒的<code>String</code>型日期。hh:mm:ss
     */
    public static String getCurrTimeStr() {
        return format(getCurrDate(), HH_MM_SS);
    }

    /**
     * 获取当前完整时间,样式: yyyy－MM－dd hh:mm:ss
     *
     * @return 一个包含年月日时分秒的<code>String</code>型日期。yyyy-MM-dd hh:mm:ss
     */
    public static String getCurrDateTimeStr() {
        return format(getCurrDate(), YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 获取当前年分 样式：yyyy
     *
     * @return 当前年分
     */
    public static String getYear() {
        return format(getCurrDate(), YYYY);
    }

    /**
     * 获取当前季度
     *
     * @return 当前季度
     */
    public static String getQuarter() {
        String quar = "";
        if (Integer.parseInt(getMonth()) == 1 || Integer.parseInt(getMonth()) == 2 || Integer.parseInt(getMonth()) == 3) {
            quar = "1";
        } else if (Integer.parseInt(getMonth()) == 4 || Integer.parseInt(getMonth()) == 5 || Integer.parseInt(getMonth()) == 6) {
            quar = "2";
        } else if (Integer.parseInt(getMonth()) == 7 || Integer.parseInt(getMonth()) == 8 || Integer.parseInt(getMonth()) == 9) {
            quar = "3";
        } else if (Integer.parseInt(getMonth()) == 10 || Integer.parseInt(getMonth()) == 11 || Integer.parseInt(getMonth()) == 12) {
            quar = "4";
        }
        return quar;
    }

    /**
     * 获取当前月分 样式：MM
     *
     * @return 当前月分
     */
    public static String getMonth() {
        return format(getCurrDate(), MM);
    }

    /**
     * 获取当前星期
     *
     * @return
     */
    public static String getWeek() {
        return format(getCurrDate(), MM);
    }

    /**
     * 获取当前星期
     *
     * @return 当前星期几
     */
    public static int getDayOfWeek() {
        Calendar aCalendar = Calendar.getInstance();
        //  里面也可以直接插入date类型
        //aCalendar.setTime(date);
        //计算此日期是一周中的哪一天
        return aCalendar.get(Calendar.DAY_OF_WEEK);
    }


    /**
     * 获取当前日期号 样式：dd
     *
     * @return 当前日期号
     */
    public static String getDay() {
        return format(getCurrDate(), DD);
    }

    /**
     * 按给定日期样式判断给定字符串是否为合法日期数据
     *
     * @param strDate 要判断的日期
     * @param pattern 日期样式
     * @return true 如果是，否则返回false
     */
    public static boolean isDate(String strDate, String pattern) {
        try {
            parse(strDate, pattern);
            return true;
        } catch (ParseException pe) {
            return false;
        }
    }

    /**
     * 判断给定字符串是否为特定格式日期（包括：年月日yyyy-MM-dd）数据
     *
     * @param strDate 要判断的日期
     * @return true 如果是，否则返回false
     */
    public static boolean isDate(String strDate) {
        try {
            parse(strDate, YYYY_MM_DD);
            return true;
        } catch (ParseException pe) {
            return false;
        }
    }

    /**
     * 判断给定字符串是否为特定格式年份（格式：yyyy）数据
     *
     * @param strDate 要判断的日期
     * @return true 如果是，否则返回false
     */
    public static boolean isYYYY(String strDate) {
        try {
            parse(strDate, YYYY);
            return true;
        } catch (ParseException pe) {
            return false;
        }
    }

    public static boolean isYYYY_MM(String strDate) {
        try {
            parse(strDate, YYYY_MM);
            return true;
        } catch (ParseException pe) {
            return false;
        }
    }

    /**
     * 判断给定字符串是否为特定格式的年月日（格式：yyyy-MM-dd）数据
     *
     * @param strDate 要判断的日期
     * @return true 如果是，否则返回false
     */
    public static boolean isYYYY_MM_DD(String strDate) {
        try {
            parse(strDate, YYYY_MM_DD);
            return true;
        } catch (ParseException pe) {
            return false;
        }
    }

    /**
     * 判断给定字符串是否为特定格式年月日时分秒（格式：yyyy-MM-dd HH:mm:ss）数据
     *
     * @param strDate 要判断的日期
     * @return true 如果是，否则返回false
     */
    public static boolean isYYYY_MM_DD_HH_MM_SS(String strDate) {
        try {
            parse(strDate, YYYY_MM_DD_HH_MM_SS);
            return true;
        } catch (ParseException pe) {
            return false;
        }
    }

    /**
     * 判断给定字符串是否为特定格式时分秒（格式：HH:mm:ss）数据
     *
     * @param strDate 要判断的日期
     * @return true 如果是，否则返回false
     */
    public static boolean isHH_MM_SS(String strDate) {
        try {
            parse(strDate, HH_MM_SS);
            return true;
        } catch (ParseException pe) {
            return false;
        }
    }

    /**
     * 判断给定字符串是否为特定格式时间（包括：时分秒hh:mm:ss）数据
     *
     * @param strTime 要判断的时间
     * @return true 如果是，否则返回false
     */
    public static boolean isTime(String strTime) {
        try {
            parse(strTime, HH_MM_SS);
            return true;
        } catch (ParseException pe) {
            return false;
        }
    }

    /**
     * 判断给定字符串是否为特定格式日期时间（包括：年月日时分秒 yyyy-MM-dd hh:mm:ss）数据
     *
     * @param strDateTime 要判断的日期时间
     * @return true 如果是，否则返回false
     */
    public static boolean isDateTime(String strDateTime) {
        try {
            parse(strDateTime, YYYY_MM_DD_HH_MM_SS);
            return true;
        } catch (ParseException pe) {
            return false;
        }
    }

    /**
     * 获取一个简单的日期格式化对象
     *
     * @return 一个简单的日期格式化对象
     */
    private static SimpleDateFormat getFormatter(String parttern) {
        return new SimpleDateFormat(parttern);
    }

    /**
     * 获取给定日前的后intevalDay天的日期
     *
     * @param refenceDate 给定日期（格式为：yyyy-MM-dd）
     * @param intevalDays 间隔天数
     * @return 计算后的日期
     */
    public static String getNextDate(String refenceDate, int intevalDays) {
        try {
            return getNextDate(parse(refenceDate, YYYY_MM_DD), intevalDays);
        } catch (Exception ee) {
            return "";
        }
    }

    /**
     * 获取给定日前的后intevalDay天的日期
     *
     * @param refenceDate Date 给定日期
     * @param intevalDays int 间隔天数
     * @return String 计算后的日期
     */
    public static String getNextDate(Date refenceDate, int intevalDays) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(refenceDate);
            calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + intevalDays);
            return format(calendar.getTime(), YYYY_MM_DD);
        } catch (Exception ee) {
            return "";
        }
    }


    public static long getIntevalDays(String startDate, String endDate) {
        try {
            return getIntevalDays(parse(startDate, YYYY_MM_DD), parse(endDate, YYYY_MM_DD));
        } catch (Exception ee) {
            return 0l;
        }
    }

    public static long getIntevalDays(Date startDate, Date endDate) {
        try {
            Calendar startCalendar = Calendar.getInstance();
            Calendar endCalendar = Calendar.getInstance();

            startCalendar.setTime(startDate);
            endCalendar.setTime(endDate);
            long diff = (long) 0.0;//endCalendar.getTimeInMillis()-startCalendar.getTimeInMillis();

            return (diff / (1000 * 60 * 60 * 24));
        } catch (Exception ee) {
            return 0l;
        }
    }

    /**
     * 求当前日期和指定字符串日期的相差天数
     *
     * @param startDate
     * @return
     */
    public static long getTodayIntevalDays(String startDate) {
        try {
            //当前时间
            Date currentDate = new Date();

            //指定日期
            SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
            Date theDate = myFormatter.parse(startDate);

            //两个时间之间的天数

            return (currentDate.getTime() - theDate.getTime()) / (24 * 60 * 60 * 1000);
        } catch (Exception ee) {
            return 0l;
        }
    }

    /**
     * 求当前日期和指定字符串日期的相差天数
     *
     * @param startDate
     * @return
     */
    public static long getTodayIntevalDays(Date startDate) {
        try {
            //当前时间
            Date currentDate = new Date();

//				//指定日期
//				SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
//				java.util.Date theDate= myFormatter.parse(startDate);

            //两个时间之间的天数

            return (currentDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
        } catch (Exception ee) {
            return 0l;
        }
    }

    public static Date parseToDate(String dateTimeStr) {
        if (dateTimeStr == null) return null;
        Date d = null;
        int formatStrLength = formatStr.length;
        for (int i = 0; i < formatStrLength; i++) {
            d = parseToDate2(dateTimeStr, formatStr[i]);
            if (d != null) {
                break;
            }
        }
        return d;
    }

    private static Date parseToDate2(String dateTimeStr, String formatString) {
        Date d = null;
        SimpleDateFormat sdf = new SimpleDateFormat(formatString);
        try {
            d = sdf.parse(dateTimeStr);
        } catch (ParseException pe) {
            pe.printStackTrace();
        }
        return d;
    }

    public static String dateTimeToString(Date datetime) {
        //dateTime=dateTime.substring(0,4)+dateTime.substring(5,7)+dateTime.substring(8,10)+dateTime.substring(11,13)+dateTime.substring(14,16)+dateTime.substring(17,19);
        //return dateTime;

        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(datetime);
        //DateFormat df = DateFormat.getDateInstance(DateFormat.LONG,Locale.CHINA);
        //System.out.println( df.format(calendar.get.getTime()) );
        //+"."+calendar.get(Calendar.MILLISECOND);
        //String dateTime=new Date().toLocaleString();

        return calendar.get(Calendar.YEAR) + "" +
                (calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0") + (calendar.get(Calendar.MONTH) + 1) + "" +
                (calendar.get(Calendar.DATE) > 9 ? "" : "0") + calendar.get(Calendar.DATE) + "" +
                (calendar.get(Calendar.HOUR_OF_DAY) > 9 ? "" : "0") + calendar.get(Calendar.HOUR_OF_DAY) + "" +
                (calendar.get(Calendar.MINUTE) > 9 ? "" : "0") + calendar.get(Calendar.MINUTE) + "" +
                (calendar.get(Calendar.SECOND) > 9 ? "" : "0") + calendar.get(Calendar.SECOND);
    }

    public static int getDayOfWeek(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(year, month - 1, day);
        //SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
        //System.err.println(formatter.format(cal.getTime()));
        int dayofWeek = cal.get(cal.DAY_OF_WEEK);
        switch (dayofWeek) {
            case 1:
                dayofWeek = 7;
                break;
            case 2:
                dayofWeek = 1;
                break;
            case 3:
                dayofWeek = 2;
                break;
            case 4:
                dayofWeek = 3;
                break;
            case 5:
                dayofWeek = 4;
                break;
            case 6:
                dayofWeek = 5;
                break;
            case 7:
                dayofWeek = 6;
                break;
        }

        return dayofWeek;
    }

    /**
     * 得到星期几
     *
     * @param day
     * @return
     */
    public static String getDayWeek(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(year, month - 1, day);
        String week = "";
        int dayofWeek = cal.get(cal.DAY_OF_WEEK);
        switch (dayofWeek) {
            case 1:
                week = "Sunday";
                break;
            case 2:
                week = "Monday";
                break;
            case 3:
                week = "Tuesday";
                break;
            case 4:
                week = "Wednesday";
                break;
            case 5:
                week = "Thursday";
                break;
            case 6:
                week = "Friday";
                break;
            case 7:
                week = "Saturday";
                break;
        }

        return week;
    }

    /**
     * @param year
     * @param mon
     * @param week
     * @return
     */
    public static String[] getMonthWeek(int year, int mon, int week) {
        String[] date = new String[2];
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.YEAR, year);
        ca.set(Calendar.MONTH, mon - 1);
        ca.set(Calendar.WEEK_OF_MONTH, week + 1);
        int day = ca.get(Calendar.DAY_OF_WEEK);
        ca.add(Calendar.DAY_OF_YEAR, 6 - day);

        date[0] = format(ca.getTime(), YYYY_MM_DD);
        ca.add(Calendar.DAY_OF_YEAR, -4);
        date[1] = format(ca.getTime(), YYYY_MM_DD);
        return date;
    }


    /**
     * 得到周下拉数据
     *
     * @param year
     * @param mon
     * @param week
     * @return
     */
    public static Vector getWeekDays(int year, int mon, int week) {
        Vector v = new Vector();
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.YEAR, year);
        ca.set(Calendar.MONTH, mon - 1);
        ca.set(Calendar.WEEK_OF_MONTH, week + 1);
        int day = ca.get(Calendar.DAY_OF_WEEK);
        ca.add(Calendar.DAY_OF_YEAR, 6 - day);
        v.add(new org.apache.struts.util.LabelValueBean("Friday", DateUtils.format(ca.getTime(), "dd")));
        ca.add(Calendar.DAY_OF_YEAR, -4);
        v.add(new org.apache.struts.util.LabelValueBean("Monday", DateUtils.format(ca.getTime(), "dd")));
        ca.add(Calendar.DAY_OF_YEAR, 1);
        v.add(new org.apache.struts.util.LabelValueBean("Tuesday", DateUtils.format(ca.getTime(), "dd")));
        ca.add(Calendar.DAY_OF_YEAR, 1);
        v.add(new org.apache.struts.util.LabelValueBean("Wednesday", DateUtils.format(ca.getTime(), "dd")));
        ca.add(Calendar.DAY_OF_YEAR, 1);
        v.add(new org.apache.struts.util.LabelValueBean("Thursday", DateUtils.format(ca.getTime(), "dd")));
        return v;
    }

    public static Date[] getWeekDay(int year, int mon, int week) {
        Date[] date = new Date[5];
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.YEAR, year);
        ca.set(Calendar.MONTH, mon - 1);
        ca.set(Calendar.WEEK_OF_MONTH, week + 1);
        int day = ca.get(Calendar.DAY_OF_WEEK);
        ca.add(Calendar.DAY_OF_YEAR, 6 - day);
        date[4] = ca.getTime();
        ca.add(Calendar.DAY_OF_YEAR, -4);
        date[0] = ca.getTime();
        ca.add(Calendar.DAY_OF_YEAR, 1);
        date[1] = ca.getTime();
        ca.add(Calendar.DAY_OF_YEAR, 1);
        date[2] = ca.getTime();
        ca.add(Calendar.DAY_OF_YEAR, 1);
        date[3] = ca.getTime();
        return date;
    }

    public static int getCurrWeek() {
        Calendar ca = Calendar.getInstance();
        return ca.get(ca.WEEK_OF_MONTH) - 1;
    }


    public static int getWeekOfYear(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(year, month - 1, day);
        //SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
        //System.err.println(formatter.format(cal.getTime()));
        return cal.get(cal.WEEK_OF_YEAR);
    }

    public static int getDayOfMonth(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(year, month - 1, day);
        //SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
        //System.err.println(formatter.format(cal.getTime()));
        return cal.get(cal.DAY_OF_MONTH);
    }

    public static String getFrontMonth(int year, int month) {
        String monthstr = "", yearstr = String.valueOf(year);
        switch (month) {
            case 1:
                monthstr = "12";
                yearstr = String.valueOf(year - 1);
                break;
            case 2:
                monthstr = "01";
                break;
            case 3:
                monthstr = "02";
                break;
            case 4:
                monthstr = "03";
                break;
            case 5:
                monthstr = "04";
                break;
            case 6:
                monthstr = "05";
                break;
            case 7:
                monthstr = "06";
                break;
            case 8:
                monthstr = "07";
                break;
            case 9:
                monthstr = "08";
                break;
            case 10:
                monthstr = "09";
                break;
            case 11:
                monthstr = "10";
                break;
            case 12:
                monthstr = "11";
                break;
        }
        return yearstr + monthstr;
    }

    public static String getFrontMonth(String month) {
        int mon = Integer.parseInt(month);
        String frontmonth = "";
        if (mon == 1) {
            frontmonth = "12";
        } else if (mon < 11) {
            frontmonth = "0" + String.valueOf(mon - 1);
        } else {
            frontmonth = String.valueOf(mon - 1);
        }
        return frontmonth;
    }


    /**
     * debug
     */
    public static void main(String[] args) {
        try {
            //System.out.println(getPreWeekDate(6));
            System.out.println(getIntevalDays("2005-01-01", "2005-01-03"));
            String f = String.valueOf(new Float(12.34));
            System.out.println(f);
            int i = f.lastIndexOf(".");
            System.out.println(f.substring(0, i));
            System.out.println(f.substring(i + 1));


            System.out.println("--------------------------------");
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String mDateTime = formatter.format(cal.getTime());
            System.out.println(mDateTime);
            System.out.println(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String getTheYesterYearDate(String date) {
        String beginDate2;

        GregorianCalendar calendar = new GregorianCalendar();

        calendar.setTime(DateUtils.format(date));
        if ((calendar.get(Calendar.MONTH) + 1) == 2 && calendar.get(Calendar.DATE) == 29) {
            beginDate2 = "" + (calendar.get(Calendar.YEAR) - 1);

            if ((calendar.get(Calendar.MONTH) + 1) < 10) {
                beginDate2 = beginDate2 + "-0" + (calendar.get(Calendar.MONTH) + 1);
            } else {
                beginDate2 = beginDate2 + "-" + (calendar.get(Calendar.MONTH) + 1);
            }

            if ((calendar.get(Calendar.DATE) - 1) < 10) {
                beginDate2 = beginDate2 + "-0" + (calendar.get(Calendar.DATE) - 1);
            } else {
                beginDate2 = beginDate2 + "-" + (calendar.get(Calendar.DATE) - 1);
            }
        } else {
            beginDate2 = "" + (calendar.get(Calendar.YEAR) - 1);

            if ((calendar.get(Calendar.MONTH) + 1) < 10) {
                beginDate2 = beginDate2 + "-0" + (calendar.get(Calendar.MONTH) + 1);
            } else {
                beginDate2 = beginDate2 + "-" + (calendar.get(Calendar.MONTH) + 1);
            }

            if ((calendar.get(Calendar.DATE)) < 10) {
                beginDate2 = beginDate2 + "-0" + (calendar.get(Calendar.DATE));
            } else {
                beginDate2 = beginDate2 + "-" + (calendar.get(Calendar.DATE));
            }
        }

        return beginDate2;
    }


    /**
     * 求当前日期和指定字符串日期的相差天数
     *
     * @param startDate
     * @return
     */
    public static long getIntevalHours(String startDate) {
        try {
            //当前时间
            Date currentDate = new Date();

            //指定日期
            SimpleDateFormat myFormatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
            Date theDate = myFormatter.parse(startDate);

            //两个时间之间的天数

            return (currentDate.getTime() - theDate.getTime()) / (60 * 60 * 1000);
        } catch (Exception ee) {
            System.out.println(ee);
            return 0l;
        }
    }

    /**
     * 求当前日期和指定字符串日期的相差天数
     *
     * @param startDate
     * @return
     */
    public static long getIntevalHours(Timestamp startDate, Timestamp endDate) {
        try {
            long hours = 0;
            if (endDate == null) {
                hours = ((new Date()).getTime() - startDate.getTime()) / (60 * 60 * 1000);
            } else {
                hours = (endDate.getTime() - startDate.getTime()) / (60 * 60 * 1000);
            }

            return hours;
        } catch (Exception ee) {
            System.out.println(ee);
            return 0;
        }
    }


    public static Vector getYears() {
        Vector v = new Vector();
        for (int i = 2007; i < 2011; i++) {
            v.add(new org.apache.struts.util.LabelValueBean(String.valueOf(i), String.valueOf(i)));
        }
        return v;
    }


    public static Vector getMonths() {
        Vector v = new Vector();
        for (int i = 1; i <= 12; i++) {
            if (i < 10) {
                v.add(new org.apache.struts.util.LabelValueBean(String.valueOf(i), "0" + String.valueOf(i)));
            } else {
                v.add(new org.apache.struts.util.LabelValueBean(String.valueOf(i), String.valueOf(i)));
            }
        }
        return v;
    }

    public static Vector getWeeks() {
        Vector v = new Vector();
        for (int i = 1; i <= 5; i++) {
            v.add(new org.apache.struts.util.LabelValueBean("第" + String.valueOf(i) + "周", String.valueOf(i)));
        }
        return v;
    }

    public static Vector getYears(String year) {
        Vector v = new Vector();
        v.add(new org.apache.struts.util.LabelValueBean(year, year));
        return v;
    }


    public static Vector getMonths(int month) {
        Vector v = new Vector();
        if (month == 1) v.add(new org.apache.struts.util.LabelValueBean("January", "0" + String.valueOf(month)));
        if (month == 2) v.add(new org.apache.struts.util.LabelValueBean("February", "0" + String.valueOf(month)));
        if (month == 3) v.add(new org.apache.struts.util.LabelValueBean("March", "0" + String.valueOf(month)));
        if (month == 4) v.add(new org.apache.struts.util.LabelValueBean("April", "0" + String.valueOf(month)));
        if (month == 5) v.add(new org.apache.struts.util.LabelValueBean("May", "0" + String.valueOf(month)));
        if (month == 6) v.add(new org.apache.struts.util.LabelValueBean("June", "0" + String.valueOf(month)));
        if (month == 7) v.add(new org.apache.struts.util.LabelValueBean("July", "0" + String.valueOf(month)));
        if (month == 8) v.add(new org.apache.struts.util.LabelValueBean("August", "0" + String.valueOf(month)));
        if (month == 9) v.add(new org.apache.struts.util.LabelValueBean("September", "0" + String.valueOf(month)));
        if (month == 10) v.add(new org.apache.struts.util.LabelValueBean("October", String.valueOf(month)));
        if (month == 11) v.add(new org.apache.struts.util.LabelValueBean("November", String.valueOf(month)));
        if (month == 12) v.add(new org.apache.struts.util.LabelValueBean("December", String.valueOf(month)));
        return v;
    }

    public static Vector getWeeks(int week) {
        Vector v = new Vector();
        if (week == 1) v.add(new org.apache.struts.util.LabelValueBean("First week", String.valueOf(week)));
        if (week == 2) v.add(new org.apache.struts.util.LabelValueBean("Second week", String.valueOf(week)));
        if (week == 3) v.add(new org.apache.struts.util.LabelValueBean("Third week", String.valueOf(week)));
        if (week == 4) v.add(new org.apache.struts.util.LabelValueBean("Fourth week", String.valueOf(week)));
        if (week == 5) v.add(new org.apache.struts.util.LabelValueBean("Fifth week", String.valueOf(week)));
        return v;
    }

    public static Vector getDays() {
        Vector v = new Vector();
        for (int i = 1; i <= 31; i++) {
            if (i < 10) {
                v.add(new org.apache.struts.util.LabelValueBean(String.valueOf(i), "0" + String.valueOf(i)));
            } else {
                v.add(new org.apache.struts.util.LabelValueBean(String.valueOf(i), String.valueOf(i)));
            }
        }
        return v;
    }

    public static Vector getHours() {
        Vector v = new Vector();
        for (int i = 0; i <= 8; i++) {
            v.add(new org.apache.struts.util.LabelValueBean(String.valueOf(i), String.valueOf(i)));
        }
        return v;
    }

    /**
     * 返回当前时区24小时制时间
     * @return eg 2017-06-13 18:37:51
     */
    public static String getNowTimeString() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(currentTime);
    }

}


